package com.demo.modules;

import cn.hutool.core.util.RandomUtil;
import com.demo.common.support.controller.AbstractDataResponseController;
import com.demo.common.support.model.DataResponse;
import com.demo.common.swagger.annotation.ApiDataResponse;
import com.demo.modules.dto.CustomAuthenticationToken;
import com.demo.modules.dto.UserLoginParamDto;
import com.demo.modules.test.user.model.User;
import com.demo.modules.test.user.service.UserService;
import com.demo.modules.test.user.utils.RandomUserUtil;
import com.demo.security.model.LoginSuccessTokenVo;
import com.demo.security.token.JwtAccessToken;
import com.demo.security.token.JwtTokenFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 测试controller
 *
 * @author molong
 * @date 2021/9/6
 */
@RestController
@Slf4j
@Api(tags = "测试测试")
public class TestController extends AbstractDataResponseController {

    @Resource
    private JwtTokenFactory jwtTokenFactory;
    @Resource
    private UserService userService;

    @ApiOperation("测试")
    @GetMapping("/test")
    public void dailyStatisticOfPassenger() {
        log.debug("hello world ！");
    }

    @ApiOperation("测试分表")
    @GetMapping("/test-separe")
    public DataResponse<Boolean> testSeparateTable() {
        LocalDateTime now=LocalDateTime.now().withMonth(1).withDayOfMonth(1);
        for (int i=0;i<=11;i++) {
            LocalDateTime datetime=now.plusMonths(i);
            for (int j=0;j<=50;j++) {
                LocalDateTime date=datetime.withDayOfMonth(RandomUtil.randomInt(26)+1);
                User user= RandomUserUtil.getRandomUser();
                user.setDataTime(date);
                userService.save(user);
            }
        }
        return succeed(true,"操作成功");
    }

    @ApiOperation("查询用户")
    @GetMapping("/list-user")
    public DataResponse<User> listUser() {
        List<User> result=userService.lambdaQuery()
                .orderByAsc(User::getDataTime)
                .list();
        return succeed(result,"操作成功");
    }


    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/login")
    @ApiDataResponse(responseType = LoginSuccessTokenVo.class)
    public DataResponse<LoginSuccessTokenVo> login(@RequestBody UserLoginParamDto loginParam) {
        //todo账号密码校验,redis缓存用户信息,authCode用于组装rediskey
        String authCode="111";
        //可以在此添加添加 验证失败次数限制
        //验证账号
        //验证密码

        CustomAuthenticationToken customAuthenticationToken = new CustomAuthenticationToken(authCode);
        JwtAccessToken accessToken = jwtTokenFactory.createAccessJwtToken(customAuthenticationToken);
        JwtAccessToken refreshToken = jwtTokenFactory.createRefreshToken(customAuthenticationToken);
        SecurityContextHolder.getContext().setAuthentication(customAuthenticationToken);

        LoginSuccessTokenVo vo = new LoginSuccessTokenVo();
        vo.setToken(accessToken.getToken());
        vo.setExp(accessToken.getExp());
        vo.setRefreshToken(refreshToken.getToken());
        vo.setRefreshTokenExp(refreshToken.getExp());
        return succeed(vo);
    }
}
